/***************************************************************************
 *                      COPYRIGHT NOTICE
 *                Copyright 2024 JamesBin, Inc.
 *                     All rights reserved.
 ***************************************************************************/

#ifndef __CLOGGER_H__
#define __CLOGGER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdint.h>
#include "log_entry_write.h"

#include "clog_common.h"
#include "share_mem.h"

#define LOG_FILE_NAME           "message"
#define LOG_HEADER_SIZE         (3)
#define LOG_FILE_PATH_LEN       (256)
#define TMP_MODULE_BUF_SIZE     (1024 * 1024 * 1) /* 1M */
#define WAIT_TIMEOUT            (110)

#define SHM_NAME    "/clog_shmem"
#define SHM_SIZE    (1024*10)   /* 10KB */


/* dump worker threads */
#define MAX_WORKER_THREADS      4   /* max worker threads, default 4 */
#define TASK_QUEUE_CAPACITY     32  /* task queue capacity, default 32 */

#define TASK_TYPE_DUMP          0   /* dump task type */
#define TASK_TYPE_FLUSH         1   /* flush task type */

#define TASK_PRIORITY_HIGH      0   /* high priority */
#define TASK_PRIORITY_LOW       1   /* low priority */

typedef struct {
    module_info_t *module;      /* module info */
    uint8_t task_type;          /* task type 0: dump, 1: flush */
    uint8_t priority;           /* task priority 0: high, 1: low */
    uint64_t enqueue_timestamp; /* enqueue timestamp */
} dump_task_t;

typedef struct {
    dump_task_t tasks[TASK_QUEUE_CAPACITY];  /* task queue */
    int32_t front;                /* front index */
    int32_t rear;                 /* rear index */
    pthread_mutex_t mutex;       /* task queue mutex */
    pthread_cond_t cond;         /* task queue cond */
} task_queue_t;

typedef struct {
    pthread_t dump_check_pthread;
    pthread_t workers[MAX_WORKER_THREADS];  /* worker threads */
    task_queue_t task_queue;     /* task queue */
    bool running;               /* thread pool running */
} thread_pool_t;

/* clog system interface */
int32_t clog_system_init_interface(const char *cfg_file);
int32_t clog_system_deinit_interface(void);
int32_t write_one_log_message_interface(shared_mem_t* shared_mem, uint8_t *data, size_t size);
int32_t write_one_log_message_to_ring_buf_interface(uint8_t *data, size_t size);
int32_t clog_write_device_init_interface(clog_entry_t *entry);
#endif
